<?php
/**
 * 一剪美公众号模块定义
 *
 * @author 指尖营销
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');
require IA_ROOT.'/addons/zh_yjm/inc/func/core.php';
class Zh_yjmModuleSite extends Core {
    public function doMobileIndex(){
        global $_W, $_GPC;
        load()->func('tpl'); 
        $banner=pdo_getall('ims_zh_yjm_banner',array('state'=>1,'uniacid'=>$_W['uniacid']));
        $storemenu=pdo_getall('ims_zh_yjm_storetype',array('state'=>1,'uniacid'=>$_W['uniacid']));
        $menus=pdo_getall('ims_zh_yjm_menu',array('state'=>1,'uniacid'=>$_W['uniacid']), array() , '' , 'sort ASC' , array());
        $total=count($menus);
        include $this->template('index');
    }
    public function doMobileMember(){
        global $_W, $_GPC;
        load()->func('tpl'); 
        $menus=pdo_getall('ims_zh_yjm_menu',array('state'=>1,'uniacid'=>$_W['uniacid']), array() , '' , 'sort ASC' , array());
        $total=count($menus);
        include $this->template('member');
    }
    public function doMobileStore(){
        global $_W, $_GPC;
        load()->func('tpl'); 
        $menus=pdo_getall('ims_zh_yjm_menu',array('state'=>1,'uniacid'=>$_W['uniacid']), array() , '' , 'sort ASC' , array());
        $total=count($menus);
        include $this->template('store');
    }
    public function doMobileAccount(){
        global $_W, $_GPC;
        load()->func('tpl'); 
        $menus=pdo_getall('ims_zh_yjm_menu',array('state'=>1,'uniacid'=>$_W['uniacid']), array() , '' , 'sort ASC' , array());
        $total=count($menus);
        include $this->template('account');
    }
    public function doMobileRegister(){
        global $_W, $_GPC;
        load()->func('tpl'); 
        $menus=pdo_getall('ims_zh_yjm_menu',array('state'=>1,'uniacid'=>$_W['uniacid']), array() , '' , 'sort ASC' , array());
        $total=count($menus);
        include $this->template('register');
    }
    public function doMobileFooter(){
        global $_W, $_GPC;
        load()->func('tpl'); 
        $menus=pdo_getall('ims_zh_yjm_menu',array('state'=>1,'uniacid'=>$_W['uniacid']), array() , '' , 'sort ASC' , array());
        $total=count($menus);
        include $this->template('footer');
    }
    //分类
    public function doMobileStorelist(){
        global $_W, $_GPC;
        $lat=$_GPC['lat'];
        $lng=$_GPC['lng'];
        $pageindex = max(1, intval($_GPC['page']));
        $pagesize=8;
        $where=' WHERE uniacid=:uniacid and storetype_id=:storetype_id and state=:state ';
        $data[':uniacid']=$_W['uniacid'];
        $data[':storetype_id']=$_GPC['storetype_id'];
        $data[':state']=1;
        $sql="select *,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-SUBSTRING_INDEX(latlng, ',', 1)*PI()/180)/2),2)+COS($lat*PI()/180)*COS(SUBSTRING_INDEX(latlng, ',', 1)*PI()/180)*POW(SIN(($lng*PI()/180-SUBSTRING_INDEX(latlng, ',', -1)*PI()/180)/2),2)))*1000) AS juli from".tablename('zh_yjm_store').$where." order by juli asc";
        $select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
        $res=pdo_fetchall($select_sql,$data);
        if($res){
            echo json_encode(array('msg'=>'成功','code'=>'200','data'=>$res),320);exit();
        }else{
            echo json_encode(array('msg'=>'失败','code'=>'501'),320);exit();
        }
    
    }
    //全部
    public function doMobileStorelista(){
        global $_W, $_GPC;
        $lat=$_GPC['lat'];
        $lng=$_GPC['lng'];
        $pageindex = max(1, intval($_GPC['page']));
        $pagesize=8;
        $where=' WHERE uniacid=:uniacid and state=:state ';
        $data[':uniacid']=$_W['uniacid'];
        $data[':state']=1;
        $sql="select *,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-SUBSTRING_INDEX(latlng, ',', 1)*PI()/180)/2),2)+COS($lat*PI()/180)*COS(SUBSTRING_INDEX(latlng, ',', 1)*PI()/180)*POW(SIN(($lng*PI()/180-SUBSTRING_INDEX(latlng, ',', -1)*PI()/180)/2),2)))*1000) AS juli from".tablename('zh_yjm_store').$where." order by juli asc";
        $select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
        $res=pdo_fetchall($select_sql,$data);
        if($res){
            echo json_encode(array('msg'=>'成功','code'=>'200','data'=>$res),320);exit();
        }else{
            echo json_encode(array('msg'=>'失败','code'=>'501'),320);exit();
        }
    
    }
    //底部自定义
    public function doMobileMenuself(){
        global $_W, $_GPC;
        $res=pdo_getall('ims_zh_yjm_menu',array('state'=>1,'uniacid'=>$_W['uniacid']), array() , '' , 'sort ASC' , array());
        if($res){
            echo json_encode(array('msg'=>'成功','code'=>'200','data'=>$res),320);exit();
        }else{
            echo json_encode(array('msg'=>'失败','code'=>'501'),320);exit();
        }
    
    }

    //商家后台记录消费获取项目
    public function doMobileProject(){
        global $_W, $_GPC;
        $res=pdo_getall('zh_yjm_project',array('uniacid'=>$_W['uniacid'],'projecttype_id'=>$_GPC['projecttype_id']),array(),'','sort asc');
        if($res){
            echo json_encode(array('msg'=>'成功','code'=>'200','data'=>$res),320);exit();
        }else{
            echo json_encode(array('msg'=>'失败','code'=>'501'),320);exit();
        }
    
    }
    //门店详情
    public function doMobileStoredetail(){
        global $_W, $_GPC;
        $res=pdo_get('zh_yjm_store',array('id'=>$_GPC['id']));
        if($res){
            echo json_encode(array('msg'=>'成功','code'=>'200','data'=>$res),320);exit();
        }else{
            echo json_encode(array('msg'=>'失败','code'=>'501'),320);exit();
        }
    
    }

    //会员门店
    public function doMobileMemberstore(){
        global $_W, $_GPC;
        $lat=$_GPC['lat'];
        $lng=$_GPC['lng'];
        // $res=pdo_getall('zh_yjm_cusmember',array('uniacid'=>$_W['uniacid'],'phone'=>$_GPC['phone']),array(),'','sort asc');
        $arr=pdo_fetchall("SELECT a.*,b.money,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-SUBSTRING_INDEX(latlng, ',', 1)*PI()/180)/2),2)+COS($lat*PI()/180)*COS(SUBSTRING_INDEX(latlng, ',', 1)*PI()/180)*POW(SIN(($lng*PI()/180-SUBSTRING_INDEX(latlng, ',', -1)*PI()/180)/2),2)))*1000) AS juli FROM ".tablename('zh_yjm_store'). " a left join " . tablename("zh_yjm_cusmember") . " b on a.id=b.store_id WHERE b.uniacid=:uniacid and b.phone=:phone ORDER BY juli desc", array(':uniacid'=>$_W['uniacid'],':phone'=>$_GPC['phone']));
        if($arr){
            echo json_encode(array('msg'=>'成功','code'=>'200','data'=>$arr),320);exit();
        }else{
            echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
        }
    
    }

    //充值记录
    public function doMobileMoneyhis(){
        global $_W, $_GPC;
        $member=pdo_get('zh_yjm_cusmember',array('uniacid'=>$_W['uniacid'],'phone'=>$_GPC['phone'],'store_id'=>$_GPC['store_id']));
        // $res=pdo_getall('zh_yjm_moneyinfo', array('m_id' => $member['id'],'uniacid' => $_W['uniacid']), array(), '', 'time DESC');

        $res=pdo_fetchall("SELECT a.*,b.assistant FROM ".tablename('zh_yjm_moneyinfo'). " a left join ". tablename("zh_yjm_assistant")." b on a.ass_id=b.id "." WHERE a.uniacid=:uniacid and a.m_id=:m_id ORDER BY a.time desc", array(':uniacid'=>$_W['uniacid'],':m_id'=>$member['id']));

        if($res){
            echo json_encode(array('msg'=>'成功','code'=>'200','data'=>$res),320);exit();
        }else{
            echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
        }
    
    }

    //消费记录
    public function doMobileMoneyhisx(){
        global $_W, $_GPC;
        $member=pdo_get('zh_yjm_cusmember',array('uniacid'=>$_W['uniacid'],'phone'=>$_GPC['phone'],'store_id'=>$_GPC['store_id']));
        // $res=pdo_getall('zh_yjm_moneyinfox', array('m_id' => $member['id'],'uniacid' => $_W['uniacid']), array(), '', 'time DESC');

        $res=pdo_fetchall("SELECT a.*,b.assistant,c.title,d.project FROM ".tablename('zh_yjm_moneyinfox'). " a left join ". tablename("zh_yjm_assistant")." b on a.ass_id=b.id left join ". tablename("zh_yjm_projecttype")." c on a.projecttype_id=c.id left join ". tablename("zh_yjm_project")." d on a.project_id=d.id "." WHERE a.uniacid=:uniacid and a.m_id=:m_id ORDER BY a.time desc", array(':uniacid'=>$_W['uniacid'],':m_id'=>$member['id']));

        if($res){
            echo json_encode(array('msg'=>'成功','code'=>'200','data'=>$res),320);exit();
        }else{
            echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
        }
    
    }
    //注册用户信息
    public function doMobileRegisteru(){
        global $_W, $_GPC;
        $data['openid']=$_W['fans']['openid'];
        $data['nickname'] = $_GPC['nickname'];
        $data['phone'] = $_GPC['phone'];
        $data['avatar'] = $_GPC['avatar'];
        $data['time'] = date('Y-m-d H:i:s',time());
        $data['uniacid']=$_W['uniacid'];
        $exist=pdo_get('zh_yjm_user',array('phone'=>$_GPC['phone'],'uniacid'=>$_W['uniacid']));
        if (empty($exist)) {
            $res=pdo_insert('zh_yjm_user',$data);
            if($res){   
                echo json_encode(array('msg'=>'提交成功','code'=>'200'),320);exit();
            }else{
                echo json_encode(array('msg'=>'提交失败','code'=>'501'),320);exit();
            }
        }else{
            echo json_encode(array('msg'=>'该用户已存在！！','code'=>'502'),320);exit();
        }
    }
    //批量删除用户
    public function doMobileDeleteuser(){
        global $_W,$_GPC;
        $res=pdo_delete("zh_yjm_user",array('id'=>$_GPC['id']));
         if($res){
            message('删除成功！', $this->createWebUrl('user'), 'success');
          }else{
            message('删除失败！','','error');
          }
    }
    //上传图片
    public function doMobileUploadimg(){
        global $_W, $_GPC;
        $destination_folder="../attachment/images/".$_W['uniacid']."/".date(Y)."/".date(m)."/";
        $imgmurl="images/".$_W['uniacid']."/".date(Y)."/".date(m)."/";
        if(!file_exists($destination_folder)) //如果文件(夹)不存在
        {
            mkdir($destination_folder,0777); //创建对应文件(夹),并设置权限为全部可读可写可操作
        }
        chmod($destination_folder , 0777);
        move_uploaded_file($_FILES['img']['tmp_name'], $destination_folder.$_FILES['img']['name']);
        echo $imgmurl.$_FILES['img']['name'];exit();
    }
    //注册判断
    public function doMobileRegisteris(){
        global $_W, $_GPC;
        $exist=pdo_get('zh_yjm_user',array('openid'=>$_W['fans']['openid'],'uniacid'=>$_W['uniacid']));
        if($exist){
            echo json_encode(array('msg'=>'该用户已存在！！','code'=>'502'),320);exit();
        }else{
            echo json_encode(array('msg'=>'该用户不存在！！','code'=>'200'),320);exit();
        }
        
    }
    //个人信息
    public function doMobileUserinfo(){
        global $_W, $_GPC;
        $exist=pdo_get('zh_yjm_user',array('openid'=>$_W['fans']['openid'],'uniacid'=>$_W['uniacid']));
        if($exist){
            echo json_encode(array('msg'=>'ok！','code'=>'200','res'=>$exist),320);exit();
        }else{
            echo json_encode(array('msg'=>'error！','code'=>'500'),320);exit();
        }
        
    }
    //logo
    public function doMobileLogo(){
        global $_W, $_GPC;
        $exist=pdo_get('zh_yjm_system',array('uniacid'=>$_W['uniacid']));
        if($exist){
            echo json_encode(array('msg'=>'ok！','code'=>'200','res'=>$exist['logo']),320);exit();
        }else{
            echo json_encode(array('msg'=>'error！','code'=>'200'),320);exit();
        }
        
    }
}